// lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_bluebell/routes/app_routes.dart';
import 'package:flutter_bluebell/utils/app_constants.dart';

import 'package:shared_preferences/shared_preferences.dart';


Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // 检查是否存在 Token，决定初始路由
  final prefs = await SharedPreferences.getInstance();
  final String? token = prefs.getString(AppConstants.tokenKey);
  final initialRoute = token != null && token.isNotEmpty
      ? AppRoutes.mainScreen // 如果有 Token，直接进入主页
      : AppRoutes.loginScreen; // 没有 Token，进入登录页

  runApp(MyApp(initialRoute: initialRoute));
}

class MyApp extends StatelessWidget {
  final String initialRoute;

  const MyApp({super.key, required this.initialRoute});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '社区应用',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: initialRoute, // 根据 Token 决定初始路由
      onGenerateRoute: AppRoutes.generateRoute,
    );
  }
}